1 00:00:00,590 --> 00:00:02,540 Okay, so I actually lied. 2 00:00:02,540 --> 00:00:07,430 There is one more thing I would like to add to our game, and that is adding a feature where we can 3 00:00:07,430 --> 00:00:12,410 save the tools of a player when they die, and give those tools back to them when they respawn in the 4 00:00:12,410 --> 00:00:12,920 game. 5 00:00:12,920 --> 00:00:18,110 This can alleviate the frustration of players losing their tools when they die and joining back into 6 00:00:18,110 --> 00:00:19,460 the game with no tools. 7 00:00:19,460 --> 00:00:24,830 Imagine if there were players on wave ten, most of them dying, and then when they spawned back into 8 00:00:24,830 --> 00:00:27,590 wave 11, they had no guns to defend themselves. 9 00:00:27,710 --> 00:00:32,660 Thankfully, this kind of system will be easy to implement, so let's go ahead and create a new server 10 00:00:32,660 --> 00:00:34,880 script inside of server Script service. 11 00:00:35,300 --> 00:00:39,950 And I'm going to call this my backpack save handler. 12 00:00:41,280 --> 00:00:45,720 We'll paste in that comment template, get rid of that section and get rid of that section. 13 00:00:46,830 --> 00:00:49,560 And then we'll go ahead and get the player service. 14 00:00:52,790 --> 00:00:56,900 And what we want to do is we want to listen for when a player is added into our game. 15 00:00:57,450 --> 00:01:01,950 And we're going to create a new folder inside of this player instance, and we can call it something 16 00:01:01,950 --> 00:01:03,180 like Backpack Save. 17 00:01:03,180 --> 00:01:07,860 So that way when our player dies, we can go ahead and move all their tools into that folder. 18 00:01:07,860 --> 00:01:13,680 And when the player respawns back on the live team, then we can go ahead and move those tools back 19 00:01:13,680 --> 00:01:16,710 into the player's backpack. 20 00:01:17,320 --> 00:01:20,710 So we'll create a new folder I'm going to call it Backpack Save. 21 00:01:20,710 --> 00:01:23,920 That's equal to instance dot new folder. 22 00:01:24,830 --> 00:01:28,220 The name is going to be equal to backpack save. 23 00:01:28,220 --> 00:01:31,220 And then we'll set the parent equal to this player. 24 00:01:31,430 --> 00:01:37,610 And then here is where we can go ahead and listen for when a character A is added to our player, get 25 00:01:37,610 --> 00:01:38,630 that character. 26 00:01:39,920 --> 00:01:43,700 And then we'll create a dedicated function to listen for when a character is added to a player. 27 00:01:43,700 --> 00:01:45,890 So we'll call it on character added. 28 00:01:45,890 --> 00:01:49,700 And we want to get past the player and the associated character. 29 00:01:49,700 --> 00:01:54,350 And then we can just call on character added pass our player, pass our character. 30 00:01:56,750 --> 00:02:02,960 And then what we can go ahead and do is when a player has a new character added to them, we want to 31 00:02:02,960 --> 00:02:05,330 check if this player is on the alive team. 32 00:02:05,330 --> 00:02:11,570 That way we can move any existing tools that are inside of their backpack safe folder into their backpack. 33 00:02:11,570 --> 00:02:19,910 So let's go ahead and grab the team service, get service teams and if this player dot team is equal 34 00:02:19,910 --> 00:02:25,880 to the alive team and they just had a character added to them, that means they respawned into the game. 35 00:02:25,880 --> 00:02:32,060 So we need to go ahead and check to see if there's any tools inside of our backpack save folder. 36 00:02:32,060 --> 00:02:35,330 So we're going to get all the children in there if there are any. 37 00:02:37,840 --> 00:02:43,270 And what we'll do is we'll set the parent of that tool equal to the player's backpack. 38 00:02:45,630 --> 00:02:49,740 The other event we want to go ahead and listen to for this character is when they die. 39 00:02:49,740 --> 00:02:54,960 So let's go ahead and make another function specifically for listening for when a humanoid dies, we'll 40 00:02:54,960 --> 00:02:56,820 call on humanoid death. 41 00:02:57,360 --> 00:03:01,290 And let's go ahead and pass a player and a character to this function. 42 00:03:01,650 --> 00:03:07,560 And then down here we'll get this character, get their humanoid, listen to the died event, and we 43 00:03:07,560 --> 00:03:09,660 can connect a lambda function. 44 00:03:09,660 --> 00:03:14,040 And then inside of here we'll call our on humanoid death function. 45 00:03:14,040 --> 00:03:16,230 Pass this player and their character. 46 00:03:17,350 --> 00:03:24,250 And now what we need to do is when this player dies, we need to verify that the game state is not in 47 00:03:24,250 --> 00:03:26,110 the game over game state. 48 00:03:26,110 --> 00:03:31,570 So what we can go ahead and do is get the game state which is equal to workspace, get attribute game 49 00:03:31,570 --> 00:03:32,080 state. 50 00:03:32,080 --> 00:03:34,870 And that means we're also going to have to grab the game state enum. 51 00:03:34,870 --> 00:03:37,180 So let's go ahead and get replicated storage. 52 00:03:41,410 --> 00:03:46,870 And then we can make a variable for the game state enum, and we'll require a replicated storage modules 53 00:03:46,870 --> 00:03:49,060 dot enums dot game state enum. 54 00:03:49,730 --> 00:03:59,720 And then what we can do is if this game state is equal to the game state enum of game over, or if this 55 00:03:59,720 --> 00:04:07,340 game state is equal to the game state enum of game over when, well then clearly we don't want to save 56 00:04:07,340 --> 00:04:10,220 the tools of this player, so we're just going to return. 57 00:04:11,220 --> 00:04:15,990 Otherwise we can go ahead and do is we can loop through all of the tools in the player's backpack and 58 00:04:15,990 --> 00:04:18,450 place them inside of the backpack save folder. 59 00:04:18,450 --> 00:04:23,460 However, we only want to do this for their guns or their med kits or their grenades because when the 60 00:04:23,460 --> 00:04:27,690 player spawns in the game, they're automatically going to be given any knife tools. 61 00:04:27,690 --> 00:04:29,820 So we don't want to save any of the knife tools. 62 00:04:29,820 --> 00:04:33,150 So we'll loop through every single tool in this player's backpack. 63 00:04:33,150 --> 00:04:34,350 Get children. 64 00:04:35,020 --> 00:04:40,390 And what we'll do is we'll check to see if this tool has the name of knife. 65 00:04:40,390 --> 00:04:44,890 So we'll find if inside of that name it has the string of knife. 66 00:04:44,890 --> 00:04:47,230 If it does then we don't want to save this tool. 67 00:04:47,230 --> 00:04:49,210 We'll just continue to the next one. 68 00:04:49,390 --> 00:04:55,900 Otherwise we can go ahead and set the parent of this tool equal to the player's backpack save folder. 69 00:04:57,250 --> 00:05:01,420 Another thing we want to check is if the player had a tool equipped. 70 00:05:01,420 --> 00:05:10,060 So we're going to check to see if their character find first child, which is a tool. 71 00:05:11,590 --> 00:05:14,770 If they don't have a tool on their character, then we'll just return. 72 00:05:14,770 --> 00:05:20,860 Otherwise, we also want to save this tool into the backpack of the player and only if it's not a knife. 73 00:05:20,860 --> 00:05:27,430 So if string.find on this tool dot name, if it has the word knife in it, then we're just going to 74 00:05:27,430 --> 00:05:28,420 return as well. 75 00:05:28,420 --> 00:05:31,510 Otherwise we can go ahead and save this tool that they have equipped. 76 00:05:31,510 --> 00:05:35,680 So tool dot parent is going to be equal to player dot backpack save. 77 00:05:36,650 --> 00:05:37,010 Now. 78 00:05:37,010 --> 00:05:42,980 Actually, a better idea I have is we should check here if the game state is actually equal to the game 79 00:05:42,980 --> 00:05:47,030 state enum of waiting, which means we're cleaning up the game. 80 00:05:47,030 --> 00:05:52,550 So that way we don't save any tools for the players that get killed during the waiting stage. 81 00:05:52,550 --> 00:05:57,830 And then what we should do is we should listen for when the workspace gets set to this game. 82 00:05:57,830 --> 00:05:58,910 State of waiting. 83 00:05:58,910 --> 00:06:03,950 And we should loop through all the players in our game and clear out any of the tools that might be 84 00:06:03,950 --> 00:06:05,060 inside of their backpack. 85 00:06:05,060 --> 00:06:06,560 Save folders. 86 00:06:06,560 --> 00:06:13,610 So let's go ahead and also listen for when the workspace get attribute change signal of game state changes. 87 00:06:13,610 --> 00:06:18,290 We'll connect a function to this and get the current game state which is equal to workspace. 88 00:06:18,290 --> 00:06:19,970 Get attribute game state. 89 00:06:21,200 --> 00:06:23,150 And if the game state. 90 00:06:24,190 --> 00:06:27,160 Is equal to the game state enum. 91 00:06:28,450 --> 00:06:29,800 Of waiting. 92 00:06:29,920 --> 00:06:32,650 Then we should loop through every single player in our game. 93 00:06:32,650 --> 00:06:34,210 So, players. 94 00:06:34,920 --> 00:06:36,420 Get players. 95 00:06:36,630 --> 00:06:40,380 And what we should do is we should refer to this players. 96 00:06:41,000 --> 00:06:47,510 Backpack save folder and then clear all children inside of that folder. 97 00:06:48,510 --> 00:06:53,100 So now this is deleting any previous tools they had from a previous game. 98 00:06:53,100 --> 00:06:58,920 And that way when a new game starts up, every player will be fresh and they won't have any previous 99 00:06:58,920 --> 00:07:01,800 tools inside of their backpack save folder. 100 00:07:02,190 --> 00:07:08,340 So now we can go ahead and test this out by creating a two player server here on the server. 101 00:07:08,340 --> 00:07:15,000 I'm going to be giving my players some money, so for their money value I'll just give them a whole 102 00:07:15,000 --> 00:07:15,900 bunch of money. 103 00:07:16,690 --> 00:07:24,190 And then what we can go ahead and do is we can spawn in these players and let's go buy some stuff for 104 00:07:24,190 --> 00:07:24,940 these players. 105 00:07:24,940 --> 00:07:27,070 So I'll just buy all of the guns. 106 00:07:27,070 --> 00:07:30,910 Boom, boom, boom, boom, boom boom, boom boom. 107 00:07:30,910 --> 00:07:32,080 Okay, perfect. 108 00:07:32,080 --> 00:07:34,180 And then we can do the same thing for this guy. 109 00:07:34,180 --> 00:07:35,800 We'll buy all the stuff in the shop. 110 00:07:35,800 --> 00:07:38,980 Boom, boom, boom, boom, boom boom boom boom. 111 00:07:38,980 --> 00:07:39,970 Okay, cool. 112 00:07:40,720 --> 00:07:46,390 So now what I want to do is I want to have one of these players die, which means when they die, all 113 00:07:46,390 --> 00:07:48,760 of these tools should be placed inside of that folder. 114 00:07:48,760 --> 00:07:53,710 And then I want to have this player win and clean up the game to allow this guy to spawn back in. 115 00:07:53,710 --> 00:07:57,250 And when he spawns back in, he should have all of his tools left. 116 00:07:57,250 --> 00:07:58,810 So let's go ahead and see what happens. 117 00:08:00,220 --> 00:08:03,670 I'm going to lead these zombies over here to come kill this guy. 118 00:08:03,670 --> 00:08:06,400 So I'm just going to go walk out of the way and they should kill him. 119 00:08:06,400 --> 00:08:06,730 Yep. 120 00:08:06,730 --> 00:08:07,000 All right. 121 00:08:07,000 --> 00:08:08,110 They're beating up. 122 00:08:08,650 --> 00:08:09,820 Hopefully they kill him. 123 00:08:12,730 --> 00:08:13,960 I'm not here. 124 00:08:16,000 --> 00:08:17,590 I'm just kind of chilling. 125 00:08:18,160 --> 00:08:19,990 He's about to die any second. 126 00:08:20,500 --> 00:08:20,920 Yep. 127 00:08:20,920 --> 00:08:21,490 Now he's dead. 128 00:08:21,490 --> 00:08:23,320 So let's go ahead and clean up all these guys. 129 00:08:31,880 --> 00:08:32,840 Three zombies left. 130 00:08:32,840 --> 00:08:33,710 Where are they at? 131 00:08:35,000 --> 00:08:35,840 And then. 132 00:08:35,840 --> 00:08:36,410 Boom! 133 00:08:36,500 --> 00:08:38,690 All right, now we're in intermission. 134 00:08:38,690 --> 00:08:44,000 So if our guy spawns back into the game just like that. 135 00:08:44,000 --> 00:08:48,470 As you can see, we got all of our tools back, so we didn't lose anything. 136 00:08:48,470 --> 00:08:51,440 And we're not forced to have to go buy new tools. 137 00:08:51,440 --> 00:08:56,120 All of them were saved and stored inside of our backpack saves folder. 138 00:08:56,600 --> 00:08:59,930 Now let's go ahead and have both of our players die. 139 00:09:00,140 --> 00:09:01,940 That way the game is over. 140 00:09:01,940 --> 00:09:06,320 We lost and then it should clean up the game and delete all of our previous tools we have. 141 00:09:06,320 --> 00:09:10,610 So that way when we spawn in to a fresh new game, we shouldn't have these tools anymore. 142 00:09:10,730 --> 00:09:14,780 Let's go ahead and wait for this intermission to end, and then we'll have both of these guys die to 143 00:09:14,780 --> 00:09:15,770 our zombies. 144 00:09:16,190 --> 00:09:18,290 We're actually, you know, we could just reset right now. 145 00:09:18,290 --> 00:09:20,720 So I'll reset this guy and I'll reset this guy. 146 00:09:22,500 --> 00:09:23,460 All right, game over. 147 00:09:23,460 --> 00:09:24,360 Zombies win! 148 00:09:26,090 --> 00:09:27,950 All right, let's go ahead and spawn in. 149 00:09:29,210 --> 00:09:32,450 And just like that, we don't have our old tools anymore. 150 00:09:32,480 --> 00:09:33,380 Very cool. 151 00:09:34,520 --> 00:09:35,120 All right. 152 00:09:35,120 --> 00:09:39,680 In the next lecture, I'm going to be playtesting through the game to make sure everything is functioning 153 00:09:39,680 --> 00:09:40,490 properly. 154 00:09:40,490 --> 00:09:42,020 Make sure you do the same. 155 00:09:42,020 --> 00:09:46,850 Play test through the game, balance out anything to your preferences, and I'll see you in the next 156 00:09:46,850 --> 00:09:47,600 lecture.